<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog :title="!form.menuId ? '新增': '修改'"
             :visible.sync="visible"
             :close-on-click-modal="false"
             width="800px"
             append-to-body>
    <el-form ref="dataForm"
             :model="form"
             :rules="rules"
             label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="上级分类"
                        prop="parentId">
            <treeselect v-model="form.parentId"
                        :options="categoryOptions"
                        :normalizer="normalizer"
                        :show-count="true"
                        @select="selectNode"
                        placeholder="选择上级分类" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称"
                        prop="name">
            <el-input v-model="form.name"
                      placeholder="请输入菜单名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否显示"
                        prop="expand">
            <el-radio-group v-model="form.expand">
              <el-radio label="1"
                        border>是</el-radio>
              <el-radio label="0"
                        border>否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序"
                        prop="sort">
            <el-input-number v-model="form.sort"
                             style="width:100%;"
                             :min="0" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12"
                v-if="form.hierarchy == 3">
          <el-form-item label="分类图片"
                        prop="extra">
            <upload-form :value="form.extra"
                         @input="inputCallback" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格模板"
                        prop="develop">
            <el-radio-group v-model="form.develop">
              <el-radio label="1"
                        border>是</el-radio>
              <el-radio label="0"
                        border>否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button type="primary"
                 @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import { fetchTree, addObj, getObj, putObj } from "@/api/admin/sys-tree"


export default {
  name: "categoryForm",
  components: { Treeselect },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 商品树选项
      categoryOptions: [],
      // 是否显示弹出层
      visible: false,
      form: {
        treeType: 3,
        hierarchy: undefined,
        parentId: undefined,
        treeId: undefined,
        name: undefined,
        expand: undefined,
        extra: undefined,
        sort: 1
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级分类不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "分类顺序不能为空", trigger: "blur" }
        ],
        expand: [
          { required: true, message: "是否显示不能为空", trigger: "blur" }
        ],
        extra: [
          { required: true, message: "请选择分类图片", trigger: "blur" }
        ],
        develop: [
          { required: true, message: "是否是规格模板不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    inputCallback (url) {
      this.form.extra = url
    },
    init (isEdit, row) {
      this.visible = true
      this.getTreeselect();
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (isEdit) {
          getObj(row.id).then(response => {
            this.form = response.data.data
          })
        } else {
          let hierarchy = undefined, parentId = 0
          if (row) {
            hierarchy = row.hierarchy + 1
            parentId = row.id
          }
          this.form = {
            treeType: 3,
            hierarchy: hierarchy,
            parentId: parentId,
            treeId: undefined,
            name: undefined,
            expand: undefined,
            extra: undefined,
            sort: 1
          }
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.form.parentId === undefined) {
            this.form.parentId = 0
          }
          if (this.form > 3) {
            return this.$message.error("最多三层商品分类")
          }
          if (this.form.treeId) {
            putObj(this.form).then(resp => {
              this.$message.success(resp.data.message)
              this.visible = false
              this.$emit('refreshDataList')
            });
          } else {
            this.form.treeType = 3
            addObj(this.form).then(resp => {
              this.$message.success(resp.data.message)
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
    /** 查询产品分类 */
    getTreeselect () {
      fetchTree({ treeType: 3 }).then(response => {
        this.categoryOptions = [];
        const baseCategory = { id: 0, name: '根分类', hierarchy: 0, children: [] };
        baseCategory.children = response.data.data || [];
        this.categoryOptions.push(baseCategory);
      });
    },
    /** 转换数据结构 */
    normalizer (node) {
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    selectNode (node) {
      this.form.hierarchy = node.hierarchy + 1
      if (this.form.hierarchy != 2) {
        this.form.extra = undefined
      }
    }
  }
};
</script>
